﻿
namespace TFSChangeLog.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.TeamFoundation.VersionControl.Client;

    /// <summary>Summary description for <see cref="ChangeSetDataAccess"/> class.</summary>
    public sealed class ChangeSetDataAccess
    {
        /// <summary>Summary description for <c>ChangeSetDataAccess</c> method.</summary>
        /// <param name="sourceFolder">TODO: Summary description for <c>sourceFolder</c> parameter of type <c>string</c>.</param>
        /// <returns>TODO: returns an object of type <c>System.Collections.Generic.List{Microsoft.TeamFoundation.VersionControl.Client.Changeset}</c>.</returns>
        public static List<Changeset> GetChangesets(string sourceFolder)
        {
            var listChanges = new List<Changeset>();
            try
            {
                var tfs = TFSConnection.GetTFSConnection();
                var vsStore = tfs.GetService<VersionControlServer>();

                var histories = vsStore.QueryHistory(
                                                     sourceFolder,
                                                     VersionSpec.Latest,
                                                     0,
                                                     RecursionType.Full,
                                                     null,
                                                     null,
                                                     null,
                                                     int.MaxValue,
                                                     true,
                                                     false,
                                                     true);
                listChanges.AddRange(histories.Cast<Changeset>());
            }
            catch (Exception)
            {
                listChanges = null;
            }

            return listChanges;
        }
    }
}
